<!doctype html>
<html lang="zh-CN" data-theme="light">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width,initial-scale=1" />
    <meta name="generator" content="VuePress 2.0.0-beta.68" />
    <meta name="theme" content="VuePress Theme Hope 2.0.0-beta.250" />
    <style>
      html {
        background: #fff;
      }

      html[data-theme="dark"] {
        background: #1d1e1f;
      }

      body {
        background: var(--bg-color);
      }
    </style>
    <script>
      const userMode = localStorage.getItem("vuepress-theme-hope-scheme");
      const systemDarkMode =
        window.matchMedia &&
        window.matchMedia("(prefers-color-scheme: dark)").matches;

      if (userMode === "dark" || (userMode !== "light" && systemDarkMode)) {
        document.documentElement.setAttribute("data-theme", "dark");
      }
    </script>
    <meta name="referrer" content="no-referrer"><link rel="icon" href="https://gitee.com/eddie-lucas/images/raw/master/img/logo.jpg"><link rel="icon" href="https://gitee.com/eddie-lucas/images/raw/master/img/logo.png" type="image/png" sizes="512x512"><link rel="icon" href="https://gitee.com/eddie-lucas/images/raw/master/img/logo.png" type="image/png" sizes="192x192"><link rel="manifest" href="/manifest.webmanifest" crossorigin="use-credentials"><meta name="theme-color" content="#46bd87"><link rel="apple-touch-icon" href="https://gitee.com/eddie-lucas/images/raw/master/img/logo.png"><meta name="apple-mobile-web-app-capable" content="yes"><meta name="apple-mobile-web-app-status-bar-style" content="black"><meta name="msapplication-TileImage" content="https://gitee.com/eddie-lucas/images/raw/master/img/logo.png"><meta name="msapplication-TileColor" content="#ffffff"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover"><title>Redis开发与运维 | 小刘Learning</title><meta name="description" content="">
    <link rel="preload" href="/assets/style-3ede7958.css" as="style"><link rel="stylesheet" href="/assets/style-3ede7958.css">
    <link rel="modulepreload" href="/assets/app-d83e1369.js"><link rel="modulepreload" href="/assets/Redis开发与运维.html-8fbbb19d.js"><link rel="modulepreload" href="/assets/Redis开发与运维.html-76744132.js"><link rel="modulepreload" href="/assets/plugin-vue_export-helper-c27b6911.js">
    <link rel="prefetch" href="/assets/aboutMe.html-72f014c7.js" as="script"><link rel="prefetch" href="/assets/index.html-69cd5f22.js" as="script"><link rel="prefetch" href="/assets/Java集合.html-f6d1e995.js" as="script"><link rel="prefetch" href="/assets/MyBatis常见问题.html-3782c8f7.js" as="script"><link rel="prefetch" href="/assets/InnoDB的Buffer Pool.html-f0ae2755.js" as="script"><link rel="prefetch" href="/assets/InnoDB的行格式和页结构.html-31890643.js" as="script"><link rel="prefetch" href="/assets/MySQL中的各种锁.html-b819061d.js" as="script"><link rel="prefetch" href="/assets/MySQL中的排序.html-5442bdf2.js" as="script"><link rel="prefetch" href="/assets/MySQL事务与MVCC.html-11dea9ac.js" as="script"><link rel="prefetch" href="/assets/MySQL优化器原理.html-44cdf1f3.js" as="script"><link rel="prefetch" href="/assets/5种IO模型.html-623f6d24.js" as="script"><link rel="prefetch" href="/assets/Netty的线程模型.html-1933240c.js" as="script"><link rel="prefetch" href="/assets/粘包拆包问题.html-e5c58280.js" as="script"><link rel="prefetch" href="/assets/AOF和RDB持久化.html-de83ad53.js" as="script"><link rel="prefetch" href="/assets/Gossip协议.html-9cc1d704.js" as="script"><link rel="prefetch" href="/assets/Redisson看门狗机制.html-8e4e6651.js" as="script"><link rel="prefetch" href="/assets/主从复制原理.html-5692886d.js" as="script"><link rel="prefetch" href="/assets/内存淘汰策略.html-2108cb44.js" as="script"><link rel="prefetch" href="/assets/缓存击穿、穿透、雪崩.html-d043509c.js" as="script"><link rel="prefetch" href="/assets/过期键的删除策略.html-86aae1a3.js" as="script"><link rel="prefetch" href="/assets/NameServer.html-6a201ec6.js" as="script"><link rel="prefetch" href="/assets/消息发送.html-4164a815.js" as="script"><link rel="prefetch" href="/assets/消息持久化.html-ad4aec32.js" as="script"><link rel="prefetch" href="/assets/Spring4.3.x源码阅读.html-ce67a2f8.js" as="script"><link rel="prefetch" href="/assets/过滤器和拦截器的区别.html-c5dbe180.js" as="script"><link rel="prefetch" href="/assets/MySQL安装.html-ce91cf4e.js" as="script"><link rel="prefetch" href="/assets/nvm_node安装.html-c02c4943.js" as="script"><link rel="prefetch" href="/assets/xxl-job源码阅读.html-4a3b4336.js" as="script"><link rel="prefetch" href="/assets/算法补充题.html-82552fdf.js" as="script"><link rel="prefetch" href="/assets/转换函数.html-4e846421.js" as="script"><link rel="prefetch" href="/assets/分布式事务.html-4015f1f4.js" as="script"><link rel="prefetch" href="/assets/深入理解Java虚拟机.html-f58d9b73.js" as="script"><link rel="prefetch" href="/assets/Redis设计与实现.html-3d2f28fe.js" as="script"><link rel="prefetch" href="/assets/中级开发（5年内） .html-fe02712d.js" as="script"><link rel="prefetch" href="/assets/初级开发（3年内）.html-669c578d.js" as="script"><link rel="prefetch" href="/assets/高级开发（8年内）.html-8e45d1a0.js" as="script"><link rel="prefetch" href="/assets/apache poi导致的oom.html-ac4f4186.js" as="script"><link rel="prefetch" href="/assets/Spring启动及扫描流程.html-dca86884.js" as="script"><link rel="prefetch" href="/assets/404.html-81a48005.js" as="script"><link rel="prefetch" href="/assets/index.html-2b6ad090.js" as="script"><link rel="prefetch" href="/assets/index.html-30245415.js" as="script"><link rel="prefetch" href="/assets/index.html-c2c088b3.js" as="script"><link rel="prefetch" href="/assets/index.html-f512ec67.js" as="script"><link rel="prefetch" href="/assets/index.html-e1891597.js" as="script"><link rel="prefetch" href="/assets/index.html-b77929dc.js" as="script"><link rel="prefetch" href="/assets/index.html-3e3e3234.js" as="script"><link rel="prefetch" href="/assets/index.html-db16fd88.js" as="script"><link rel="prefetch" href="/assets/index.html-02e59ce2.js" as="script"><link rel="prefetch" href="/assets/index.html-55c7e4fd.js" as="script"><link rel="prefetch" href="/assets/index.html-3b089dfb.js" as="script"><link rel="prefetch" href="/assets/index.html-ac5b8d61.js" as="script"><link rel="prefetch" href="/assets/index.html-a2f3ea26.js" as="script"><link rel="prefetch" href="/assets/index.html-b57e51cb.js" as="script"><link rel="prefetch" href="/assets/index.html-0c321714.js" as="script"><link rel="prefetch" href="/assets/index.html-6266e407.js" as="script"><link rel="prefetch" href="/assets/index.html-00f53358.js" as="script"><link rel="prefetch" href="/assets/index.html-7b46e405.js" as="script"><link rel="prefetch" href="/assets/index.html-219b1b1f.js" as="script"><link rel="prefetch" href="/assets/index.html-ecf569c4.js" as="script"><link rel="prefetch" href="/assets/aboutMe.html-a5377517.js" as="script"><link rel="prefetch" href="/assets/index.html-df94614a.js" as="script"><link rel="prefetch" href="/assets/Java集合.html-25980358.js" as="script"><link rel="prefetch" href="/assets/MyBatis常见问题.html-13559d56.js" as="script"><link rel="prefetch" href="/assets/InnoDB的Buffer Pool.html-2608d2bb.js" as="script"><link rel="prefetch" href="/assets/InnoDB的行格式和页结构.html-b66829d8.js" as="script"><link rel="prefetch" href="/assets/MySQL中的各种锁.html-eda1f1e8.js" as="script"><link rel="prefetch" href="/assets/MySQL中的排序.html-155835e9.js" as="script"><link rel="prefetch" href="/assets/MySQL事务与MVCC.html-5203098d.js" as="script"><link rel="prefetch" href="/assets/MySQL优化器原理.html-580192b8.js" as="script"><link rel="prefetch" href="/assets/5种IO模型.html-e14d32e5.js" as="script"><link rel="prefetch" href="/assets/Netty的线程模型.html-79ec3f0d.js" as="script"><link rel="prefetch" href="/assets/粘包拆包问题.html-ad117100.js" as="script"><link rel="prefetch" href="/assets/AOF和RDB持久化.html-8f530297.js" as="script"><link rel="prefetch" href="/assets/Gossip协议.html-68ce5f80.js" as="script"><link rel="prefetch" href="/assets/Redisson看门狗机制.html-f806e761.js" as="script"><link rel="prefetch" href="/assets/主从复制原理.html-c35d3468.js" as="script"><link rel="prefetch" href="/assets/内存淘汰策略.html-58163b51.js" as="script"><link rel="prefetch" href="/assets/缓存击穿、穿透、雪崩.html-d8d78972.js" as="script"><link rel="prefetch" href="/assets/过期键的删除策略.html-b2be5f51.js" as="script"><link rel="prefetch" href="/assets/NameServer.html-6ada07a1.js" as="script"><link rel="prefetch" href="/assets/消息发送.html-08920331.js" as="script"><link rel="prefetch" href="/assets/消息持久化.html-32746c2c.js" as="script"><link rel="prefetch" href="/assets/Spring4.3.x源码阅读.html-3475e320.js" as="script"><link rel="prefetch" href="/assets/过滤器和拦截器的区别.html-56c13477.js" as="script"><link rel="prefetch" href="/assets/MySQL安装.html-c7803a3a.js" as="script"><link rel="prefetch" href="/assets/nvm_node安装.html-736601b9.js" as="script"><link rel="prefetch" href="/assets/xxl-job源码阅读.html-2b98669d.js" as="script"><link rel="prefetch" href="/assets/算法补充题.html-37debf9d.js" as="script"><link rel="prefetch" href="/assets/转换函数.html-44dfb333.js" as="script"><link rel="prefetch" href="/assets/分布式事务.html-e6634dbe.js" as="script"><link rel="prefetch" href="/assets/深入理解Java虚拟机.html-3810cea1.js" as="script"><link rel="prefetch" href="/assets/Redis设计与实现.html-eccc56a7.js" as="script"><link rel="prefetch" href="/assets/中级开发（5年内） .html-a2fc4d16.js" as="script"><link rel="prefetch" href="/assets/初级开发（3年内）.html-e48a4104.js" as="script"><link rel="prefetch" href="/assets/高级开发（8年内）.html-9138cbdd.js" as="script"><link rel="prefetch" href="/assets/apache poi导致的oom.html-b39dacda.js" as="script"><link rel="prefetch" href="/assets/Spring启动及扫描流程.html-0b604fff.js" as="script"><link rel="prefetch" href="/assets/404.html-742f0851.js" as="script"><link rel="prefetch" href="/assets/index.html-160c66d3.js" as="script"><link rel="prefetch" href="/assets/index.html-bb7b1e80.js" as="script"><link rel="prefetch" href="/assets/index.html-116fe317.js" as="script"><link rel="prefetch" href="/assets/index.html-17f24df8.js" as="script"><link rel="prefetch" href="/assets/index.html-3fe5d15e.js" as="script"><link rel="prefetch" href="/assets/index.html-c894bb5e.js" as="script"><link rel="prefetch" href="/assets/index.html-25b31507.js" as="script"><link rel="prefetch" href="/assets/index.html-8bb87106.js" as="script"><link rel="prefetch" href="/assets/index.html-255fc5d5.js" as="script"><link rel="prefetch" href="/assets/index.html-373a73ca.js" as="script"><link rel="prefetch" href="/assets/index.html-d539e1a9.js" as="script"><link rel="prefetch" href="/assets/index.html-1475787c.js" as="script"><link rel="prefetch" href="/assets/index.html-8073adbe.js" as="script"><link rel="prefetch" href="/assets/index.html-2ff9332c.js" as="script"><link rel="prefetch" href="/assets/index.html-f14ceb6a.js" as="script"><link rel="prefetch" href="/assets/index.html-54082429.js" as="script"><link rel="prefetch" href="/assets/index.html-264a3de2.js" as="script"><link rel="prefetch" href="/assets/index.html-fb62746d.js" as="script"><link rel="prefetch" href="/assets/index.html-81d28dad.js" as="script"><link rel="prefetch" href="/assets/index.html-56062662.js" as="script"><link rel="prefetch" href="/assets/giscus-0b7adcf8.js" as="script"><link rel="prefetch" href="/assets/photoswipe.esm-1464cdb9.js" as="script"><link rel="prefetch" href="/assets/index-e32a7948.js" as="script">
  </head>
  <body>
    <div id="app"><!--[--><!--[--><!--[--><span tabindex="-1"></span><a href="#main-content" class="vp-skip-link sr-only">跳至主要內容</a><!--]--><!--[--><div class="theme-container has-toc"><!--[--><header id="navbar" class="vp-navbar"><div class="vp-navbar-start"><button type="button" class="vp-toggle-sidebar-button" title="Toggle Sidebar"><span class="icon"></span></button><!--[--><!----><!--]--><!--[--><a class="vp-link vp-brand vp-brand" href="/"><img class="vp-nav-logo" src="https://gitee.com/eddie-lucas/images/raw/master/img/logo.svg" alt="小刘Learning"><!----><span class="vp-site-name hide-in-pad">小刘Learning</span></a><!--]--><!--[--><!----><!--]--></div><div class="vp-navbar-center"><!--[--><!----><!--]--><!--[--><nav class="vp-nav-links"><div class="nav-item hide-in-mobile"><a aria-label="首页" class="vp-link nav-link nav-link" href="/"><span class="font-icon icon iconfont icon-shouye" style=""></span>首页<!----></a></div><div class="nav-item hide-in-mobile"><a aria-label="后端技术" class="vp-link nav-link nav-link" href="/backend/mysql/MySQL%E4%BA%8B%E5%8A%A1%E4%B8%8EMVCC.html"><span class="font-icon icon iconfont icon-xingqiu" style=""></span>后端技术<!----></a></div><div class="nav-item hide-in-mobile"><a aria-label="读书笔记" class="vp-link nav-link active nav-link active" href="/readNote/redis/Redis%E5%BC%80%E5%8F%91%E4%B8%8E%E8%BF%90%E7%BB%B4.html"><span class="font-icon icon iconfont icon-book" style=""></span>读书笔记<!----></a></div></nav><!--]--><!--[--><!----><!--]--></div><div class="vp-navbar-end"><!--[--><!----><!--]--><!--[--><!----><div class="nav-item vp-repo"><a class="vp-repo-link" href="https://github.com/eddie-lucas" target="_blank" rel="noopener noreferrer" aria-label="GitHub"><svg xmlns="http://www.w3.org/2000/svg" class="icon github-icon" viewBox="0 0 1024 1024" fill="currentColor" aria-label="github icon" style="width:1.25rem;height:1.25rem;vertical-align:middle;"><path d="M511.957 21.333C241.024 21.333 21.333 240.981 21.333 512c0 216.832 140.544 400.725 335.574 465.664 24.49 4.395 32.256-10.07 32.256-23.083 0-11.69.256-44.245 0-85.205-136.448 29.61-164.736-64.64-164.736-64.64-22.315-56.704-54.4-71.765-54.4-71.765-44.587-30.464 3.285-29.824 3.285-29.824 49.195 3.413 75.179 50.517 75.179 50.517 43.776 75.008 114.816 53.333 142.762 40.79 4.523-31.66 17.152-53.377 31.19-65.537-108.971-12.458-223.488-54.485-223.488-242.602 0-53.547 19.114-97.323 50.517-131.67-5.035-12.33-21.93-62.293 4.779-129.834 0 0 41.258-13.184 134.912 50.346a469.803 469.803 0 0 1 122.88-16.554c41.642.213 83.626 5.632 122.88 16.554 93.653-63.488 134.784-50.346 134.784-50.346 26.752 67.541 9.898 117.504 4.864 129.834 31.402 34.347 50.474 78.123 50.474 131.67 0 188.586-114.73 230.016-224.042 242.09 17.578 15.232 33.578 44.672 33.578 90.454v135.85c0 13.142 7.936 27.606 32.854 22.87C862.25 912.597 1002.667 728.747 1002.667 512c0-271.019-219.648-490.667-490.71-490.667z"></path></svg></a></div><div class="nav-item hide-in-mobile"><button type="button" id="appearance-switch"><svg xmlns="http://www.w3.org/2000/svg" class="icon auto-icon" viewBox="0 0 1024 1024" fill="currentColor" aria-label="auto icon" style="display:block;"><path d="M512 992C246.92 992 32 777.08 32 512S246.92 32 512 32s480 214.92 480 480-214.92 480-480 480zm0-840c-198.78 0-360 161.22-360 360 0 198.84 161.22 360 360 360s360-161.16 360-360c0-198.78-161.22-360-360-360zm0 660V212c165.72 0 300 134.34 300 300 0 165.72-134.28 300-300 300z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" viewBox="0 0 1024 1024" fill="currentColor" aria-label="dark icon" style="display:none;"><path d="M524.8 938.667h-4.267a439.893 439.893 0 0 1-313.173-134.4 446.293 446.293 0 0 1-11.093-597.334A432.213 432.213 0 0 1 366.933 90.027a42.667 42.667 0 0 1 45.227 9.386 42.667 42.667 0 0 1 10.24 42.667 358.4 358.4 0 0 0 82.773 375.893 361.387 361.387 0 0 0 376.747 82.774 42.667 42.667 0 0 1 54.187 55.04 433.493 433.493 0 0 1-99.84 154.88 438.613 438.613 0 0 1-311.467 128z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" viewBox="0 0 1024 1024" fill="currentColor" aria-label="light icon" style="display:none;"><path d="M952 552h-80a40 40 0 0 1 0-80h80a40 40 0 0 1 0 80zM801.88 280.08a41 41 0 0 1-57.96-57.96l57.96-58a41.04 41.04 0 0 1 58 58l-58 57.96zM512 752a240 240 0 1 1 0-480 240 240 0 0 1 0 480zm0-560a40 40 0 0 1-40-40V72a40 40 0 0 1 80 0v80a40 40 0 0 1-40 40zm-289.88 88.08-58-57.96a41.04 41.04 0 0 1 58-58l57.96 58a41 41 0 0 1-57.96 57.96zM192 512a40 40 0 0 1-40 40H72a40 40 0 0 1 0-80h80a40 40 0 0 1 40 40zm30.12 231.92a41 41 0 0 1 57.96 57.96l-57.96 58a41.04 41.04 0 0 1-58-58l58-57.96zM512 832a40 40 0 0 1 40 40v80a40 40 0 0 1-80 0v-80a40 40 0 0 1 40-40zm289.88-88.08 58 57.96a41.04 41.04 0 0 1-58 58l-57.96-58a41 41 0 0 1 57.96-57.96z"></path></svg></button></div><!----><!--]--><!--[--><!----><!--]--><button type="button" class="vp-toggle-navbar-button" aria-label="Toggle Navbar" aria-expanded="false" aria-controls="nav-screen"><span><span class="vp-top"></span><span class="vp-middle"></span><span class="vp-bottom"></span></span></button></div></header><!----><!--]--><!----><div class="toggle-sidebar-wrapper"><span class="arrow start"></span></div><aside id="sidebar" class="vp-sidebar"><!--[--><!----><!--]--><ul class="vp-sidebar-links"><li><section class="vp-sidebar-group"><button class="vp-sidebar-heading clickable active" type="button"><span class="font-icon icon iconfont icon-redis" style=""></span><span class="vp-sidebar-title">Redis</span><span class="vp-arrow down"></span></button><ul class="vp-sidebar-links"><li><!--[--><a aria-label="Redis开发与运维" class="vp-link nav-link active vp-sidebar-link vp-sidebar-page active nav-link active vp-sidebar-link vp-sidebar-page active" href="/readNote/redis/Redis%E5%BC%80%E5%8F%91%E4%B8%8E%E8%BF%90%E7%BB%B4.html"><!---->Redis开发与运维<!----></a><ul class="vp-sidebar-sub-headers"><li class="vp-sidebar-sub-header"><a aria-label="全局命令" class="vp-link nav-link vp-sidebar-link vp-heading nav-link vp-sidebar-link vp-heading" href="/readNote/redis/Redis%E5%BC%80%E5%8F%91%E4%B8%8E%E8%BF%90%E7%BB%B4.html#全局命令"><!---->全局命令<!----></a><ul class="vp-sidebar-sub-headers"></ul></li><li class="vp-sidebar-sub-header"><a aria-label="数据类型和内部编码" class="vp-link nav-link vp-sidebar-link vp-heading nav-link vp-sidebar-link vp-heading" href="/readNote/redis/Redis%E5%BC%80%E5%8F%91%E4%B8%8E%E8%BF%90%E7%BB%B4.html#数据类型和内部编码"><!---->数据类型和内部编码<!----></a><ul class="vp-sidebar-sub-headers"></ul></li><li class="vp-sidebar-sub-header"><a aria-label="string" class="vp-link nav-link vp-sidebar-link vp-heading nav-link vp-sidebar-link vp-heading" href="/readNote/redis/Redis%E5%BC%80%E5%8F%91%E4%B8%8E%E8%BF%90%E7%BB%B4.html#string"><!---->string<!----></a><ul class="vp-sidebar-sub-headers"></ul></li><li class="vp-sidebar-sub-header"><a aria-label="hash" class="vp-link nav-link vp-sidebar-link vp-heading nav-link vp-sidebar-link vp-heading" href="/readNote/redis/Redis%E5%BC%80%E5%8F%91%E4%B8%8E%E8%BF%90%E7%BB%B4.html#hash"><!---->hash<!----></a><ul class="vp-sidebar-sub-headers"></ul></li><li class="vp-sidebar-sub-header"><a aria-label="list" class="vp-link nav-link vp-sidebar-link vp-heading nav-link vp-sidebar-link vp-heading" href="/readNote/redis/Redis%E5%BC%80%E5%8F%91%E4%B8%8E%E8%BF%90%E7%BB%B4.html#list"><!---->list<!----></a><ul class="vp-sidebar-sub-headers"></ul></li><li class="vp-sidebar-sub-header"><a aria-label="set" class="vp-link nav-link vp-sidebar-link vp-heading nav-link vp-sidebar-link vp-heading" href="/readNote/redis/Redis%E5%BC%80%E5%8F%91%E4%B8%8E%E8%BF%90%E7%BB%B4.html#set"><!---->set<!----></a><ul class="vp-sidebar-sub-headers"></ul></li><li class="vp-sidebar-sub-header"><a aria-label="zset" class="vp-link nav-link vp-sidebar-link vp-heading nav-link vp-sidebar-link vp-heading" href="/readNote/redis/Redis%E5%BC%80%E5%8F%91%E4%B8%8E%E8%BF%90%E7%BB%B4.html#zset"><!---->zset<!----></a><ul class="vp-sidebar-sub-headers"></ul></li><li class="vp-sidebar-sub-header"><a aria-label="理解内存" class="vp-link nav-link vp-sidebar-link vp-heading nav-link vp-sidebar-link vp-heading" href="/readNote/redis/Redis%E5%BC%80%E5%8F%91%E4%B8%8E%E8%BF%90%E7%BB%B4.html#理解内存"><!---->理解内存<!----></a><ul class="vp-sidebar-sub-headers"><li class="vp-sidebar-sub-header"><a aria-label="内存消耗" class="vp-link nav-link vp-sidebar-link vp-heading nav-link vp-sidebar-link vp-heading" href="/readNote/redis/Redis%E5%BC%80%E5%8F%91%E4%B8%8E%E8%BF%90%E7%BB%B4.html#内存消耗"><!---->内存消耗<!----></a><ul class="vp-sidebar-sub-headers"></ul></li></ul></li><li class="vp-sidebar-sub-header"><a aria-label="哨兵模式" class="vp-link nav-link vp-sidebar-link vp-heading nav-link vp-sidebar-link vp-heading" href="/readNote/redis/Redis%E5%BC%80%E5%8F%91%E4%B8%8E%E8%BF%90%E7%BB%B4.html#哨兵模式"><!---->哨兵模式<!----></a><ul class="vp-sidebar-sub-headers"><li class="vp-sidebar-sub-header"><a aria-label="redis sentinel原理" class="vp-link nav-link vp-sidebar-link vp-heading nav-link vp-sidebar-link vp-heading" href="/readNote/redis/Redis%E5%BC%80%E5%8F%91%E4%B8%8E%E8%BF%90%E7%BB%B4.html#redis-sentinel原理"><!---->redis sentinel原理<!----></a><ul class="vp-sidebar-sub-headers"></ul></li></ul></li></ul><!--]--></li><li><!--[--><a aria-label="Redis设计与实现" class="vp-link nav-link vp-sidebar-link vp-sidebar-page nav-link vp-sidebar-link vp-sidebar-page" href="/readNote/redis/Redis%E8%AE%BE%E8%AE%A1%E4%B8%8E%E5%AE%9E%E7%8E%B0.html"><!---->Redis设计与实现<!----></a><ul class="vp-sidebar-sub-headers"></ul><!--]--></li></ul></section></li><li><section class="vp-sidebar-group"><button class="vp-sidebar-heading clickable" type="button"><span class="font-icon icon iconfont icon-daohang_JVM" style=""></span><span class="vp-sidebar-title">JVM</span><span class="vp-arrow end"></span></button><!----></section></li><li><section class="vp-sidebar-group"><button class="vp-sidebar-heading clickable" type="button"><span class="font-icon icon iconfont icon-suanfayanshi" style=""></span><span class="vp-sidebar-title">算法</span><span class="vp-arrow end"></span></button><!----></section></li><li><section class="vp-sidebar-group"><button class="vp-sidebar-heading clickable" type="button"><span class="font-icon icon iconfont icon-fenbushi" style=""></span><span class="vp-sidebar-title">分布式</span><span class="vp-arrow end"></span></button><!----></section></li><li><section class="vp-sidebar-group"><button class="vp-sidebar-heading clickable" type="button"><span class="font-icon icon iconfont icon-daohang-jishutupu" style=""></span><span class="vp-sidebar-title">技术图谱</span><span class="vp-arrow end"></span></button><!----></section></li></ul><!--[--><!----><!--]--></aside><!--[--><main id="main-content" class="vp-page"><!--[--><!--[--><!----><!--]--><!----><nav class="vp-breadcrumb disable"></nav><div class="vp-page-title"><h1><!---->Redis开发与运维</h1><div class="page-info"><span class="page-author-info" aria-label="作者🖊" data-balloon-pos="down"><svg xmlns="http://www.w3.org/2000/svg" class="icon author-icon" viewBox="0 0 1024 1024" fill="currentColor" aria-label="author icon"><path d="M649.6 633.6c86.4-48 147.2-144 147.2-249.6 0-160-128-288-288-288s-288 128-288 288c0 108.8 57.6 201.6 147.2 249.6-121.6 48-214.4 153.6-240 288-3.2 9.6 0 19.2 6.4 25.6 3.2 9.6 12.8 12.8 22.4 12.8h704c9.6 0 19.2-3.2 25.6-12.8 6.4-6.4 9.6-16 6.4-25.6-25.6-134.4-121.6-240-243.2-288z"></path></svg><span><span class="page-author-item">小刘Learning</span></span><span property="author" content="小刘Learning"></span></span><!----><span class="page-date-info" aria-label="写作日期📅" data-balloon-pos="down"><svg xmlns="http://www.w3.org/2000/svg" class="icon calendar-icon" viewBox="0 0 1024 1024" fill="currentColor" aria-label="calendar icon"><path d="M716.4 110.137c0-18.753-14.72-33.473-33.472-33.473-18.753 0-33.473 14.72-33.473 33.473v33.473h66.993v-33.473zm-334.87 0c0-18.753-14.72-33.473-33.473-33.473s-33.52 14.72-33.52 33.473v33.473h66.993v-33.473zm468.81 33.52H716.4v100.465c0 18.753-14.72 33.473-33.472 33.473a33.145 33.145 0 01-33.473-33.473V143.657H381.53v100.465c0 18.753-14.72 33.473-33.473 33.473a33.145 33.145 0 01-33.473-33.473V143.657H180.6A134.314 134.314 0 0046.66 277.595v535.756A134.314 134.314 0 00180.6 947.289h669.74a134.36 134.36 0 00133.94-133.938V277.595a134.314 134.314 0 00-133.94-133.938zm33.473 267.877H147.126a33.145 33.145 0 01-33.473-33.473c0-18.752 14.72-33.473 33.473-33.473h736.687c18.752 0 33.472 14.72 33.472 33.473a33.145 33.145 0 01-33.472 33.473z"></path></svg><span><!----></span><meta property="datePublished" content="2024-05-01T13:43:04.000Z"></span><!----><span class="page-reading-time-info" aria-label="阅读时间⌛" data-balloon-pos="down"><svg xmlns="http://www.w3.org/2000/svg" class="icon timer-icon" viewBox="0 0 1024 1024" fill="currentColor" aria-label="timer icon"><path d="M799.387 122.15c4.402-2.978 7.38-7.897 7.38-13.463v-1.165c0-8.933-7.38-16.312-16.312-16.312H256.33c-8.933 0-16.311 7.38-16.311 16.312v1.165c0 5.825 2.977 10.874 7.637 13.592 4.143 194.44 97.22 354.963 220.201 392.763-122.204 37.542-214.893 196.511-220.2 389.397-4.661 5.049-7.638 11.651-7.638 19.03v5.825h566.49v-5.825c0-7.379-2.849-13.981-7.509-18.9-5.049-193.016-97.867-351.985-220.2-389.527 123.24-37.67 216.446-198.453 220.588-392.892zM531.16 450.445v352.632c117.674 1.553 211.787 40.778 211.787 88.676H304.097c0-48.286 95.149-87.382 213.728-88.676V450.445c-93.077-3.107-167.901-81.297-167.901-177.093 0-8.803 6.99-15.793 15.793-15.793 8.803 0 15.794 6.99 15.794 15.793 0 80.261 63.69 145.635 142.01 145.635s142.011-65.374 142.011-145.635c0-8.803 6.99-15.793 15.794-15.793s15.793 6.99 15.793 15.793c0 95.019-73.789 172.82-165.96 177.093z"></path></svg><span>大约 14 分钟</span><meta property="timeRequired" content="PT14M"></span><!----><!----></div><hr></div><div class="toc-place-holder"><aside id="toc"><!--[--><!----><!--]--><div class="toc-header">此页内容<button type="button" class="print-button" title="打印"><svg xmlns="http://www.w3.org/2000/svg" class="icon print-icon" viewBox="0 0 1024 1024" fill="currentColor" aria-label="print icon"><path d="M819.2 364.8h-44.8V128c0-17.067-14.933-32-32-32H281.6c-17.067 0-32 14.933-32 32v236.8h-44.8C145.067 364.8 96 413.867 96 473.6v192c0 59.733 49.067 108.8 108.8 108.8h44.8V896c0 17.067 14.933 32 32 32h460.8c17.067 0 32-14.933 32-32V774.4h44.8c59.733 0 108.8-49.067 108.8-108.8v-192c0-59.733-49.067-108.8-108.8-108.8zM313.6 160h396.8v204.8H313.6V160zm396.8 704H313.6V620.8h396.8V864zM864 665.6c0 25.6-19.2 44.8-44.8 44.8h-44.8V588.8c0-17.067-14.933-32-32-32H281.6c-17.067 0-32 14.933-32 32v121.6h-44.8c-25.6 0-44.8-19.2-44.8-44.8v-192c0-25.6 19.2-44.8 44.8-44.8h614.4c25.6 0 44.8 19.2 44.8 44.8v192z"></path></svg></button></div><div class="toc-wrapper"><ul class="toc-list"><!--[--><li class="toc-item"><a class="vp-link toc-link level2 toc-link level2" href="#全局命令">全局命令</a></li><!----><!--]--><!--[--><li class="toc-item"><a class="vp-link toc-link level2 toc-link level2" href="#数据类型和内部编码">数据类型和内部编码</a></li><!----><!--]--><!--[--><li class="toc-item"><a class="vp-link toc-link level2 toc-link level2" href="#string">string</a></li><!----><!--]--><!--[--><li class="toc-item"><a class="vp-link toc-link level2 toc-link level2" href="#hash">hash</a></li><!----><!--]--><!--[--><li class="toc-item"><a class="vp-link toc-link level2 toc-link level2" href="#list">list</a></li><!----><!--]--><!--[--><li class="toc-item"><a class="vp-link toc-link level2 toc-link level2" href="#set">set</a></li><!----><!--]--><!--[--><li class="toc-item"><a class="vp-link toc-link level2 toc-link level2" href="#zset">zset</a></li><!----><!--]--><!--[--><li class="toc-item"><a class="vp-link toc-link level2 toc-link level2" href="#理解内存">理解内存</a></li><li><ul class="toc-list"><!--[--><li class="toc-item"><a class="vp-link toc-link level3 toc-link level3" href="#内存消耗">内存消耗</a></li><!----><!--]--></ul></li><!--]--><!--[--><li class="toc-item"><a class="vp-link toc-link level2 toc-link level2" href="#哨兵模式">哨兵模式</a></li><li><ul class="toc-list"><!--[--><li class="toc-item"><a class="vp-link toc-link level3 toc-link level3" href="#redis-sentinel原理">redis sentinel原理</a></li><!----><!--]--></ul></li><!--]--></ul><div class="toc-marker" style="top:-1.7rem;"></div></div><!--[--><!----><!--]--></aside></div><!--[--><!----><!--]--><div class="theme-hope-content"><h2 id="全局命令" tabindex="-1"><a class="header-anchor" href="#全局命令" aria-hidden="true">#</a> 全局命令</h2><p>redis有多种数据结构，redis是一种键值对数据库，对于键来说，有一些通用的命令</p><ul><li><code>keys *</code> 查看所有键</li><li><code>dbsize</code>获取键的总数</li></ul><p><code>dbsize</code>在计算键的总数时，不会遍历所有的键，而是直接获取redis内置的键总数的变量，它的时间复杂度是O(1)</p><p><code>keys *</code>会遍历所有的键，所以它的时间复杂度是O(n)，当redis保存了大量的key的时候，使用该命令会消耗大量时间，线上环境禁用</p><ul><li><code>exists key</code>检查键是否存在，存在返回1，不存在返回0</li><li><code>del key [key1 key2...]</code>删除key，返回删除的key的数量，如果key不存在，返回0</li><li><code>expires key seconds</code>为键设置过期时间</li><li><code>ttl key</code>查询键的剩余过期时间，有三种返回类型 <ul><li>大于0：剩余过期时间</li><li>-1：键没有设置过期时间</li><li>-2：键不存在</li></ul></li><li><code>type key</code>键的类型，如果键不存在，返回none</li></ul><h2 id="数据类型和内部编码" tabindex="-1"><a class="header-anchor" href="#数据类型和内部编码" aria-hidden="true">#</a> 数据类型和内部编码</h2><p>reidis有多种数据类型，每种数据类型，都有自己底层的内部底层编码实现，而且是多种实现，redis会在合适的场景选择合适的编码，可以通过<code>object encoding key</code>查询内部编码，即内部实现方式，数据类型和内部编码的关系就好像接口和实现类</p><figure><img src="https://gitee.com/eddie-lucas/images/raw/master/img/image-20240501145517082.png" alt="" tabindex="0" loading="lazy"><figcaption></figcaption></figure><h2 id="string" tabindex="-1"><a class="header-anchor" href="#string" aria-hidden="true">#</a> string</h2><p>字符串是redis中一个基本的数据类型，<strong>键都是字符串类型的</strong>，字符串类型的值可以是字符串（简单的字符串，复杂的字符串（JSON，XML）），数字（整数，浮点数），甚至是二进制（音视频，图片），但是<strong>值最大不能超过512MB</strong></p><figure><img src="https://gitee.com/eddie-lucas/images/raw/master/img/image-20240501150942224.png" alt="" tabindex="0" loading="lazy"><figcaption></figcaption></figure><blockquote><p>内部编码</p></blockquote><p>字符串类型内部编码有3种，redis会根据当前值的类型和长度决定使用哪种内部编码：</p><ul><li>int：当前值为整型，长度&lt;=8字节，使用int类型的内部编码</li><li>embstr：当前值为字符串，长度&lt;=39字节，使用embstr内部编码</li><li>raw：长度&gt;39字节的字符串</li></ul><blockquote><p>使用场景</p></blockquote><p>缓存，计数，共享session，限制频率（例如一分钟只能获取一次验证码）</p><h2 id="hash" tabindex="-1"><a class="header-anchor" href="#hash" aria-hidden="true">#</a> hash</h2><p>hash是非常适合用来存储对象的，hash的一个key可以对应多个（field，value）</p><figure><img src="https://gitee.com/eddie-lucas/images/raw/master/img/image-20240501190252205.png" alt="" tabindex="0" loading="lazy"><figcaption></figcaption></figure><blockquote><p>获取全部field-value，使用hscan</p></blockquote><p>可以通过<code>hgetall key</code>获取该key对应的所有的field-value，但是，如果hash的元素比较多，会存在阻塞redis的可能，时间复杂度为O(n)，如果只获取部分field-value，可以使用<code>hmget</code>，如果一定要获取全部的field-value，可以使用<code>hscan</code>，该命令会<strong>渐进式的遍历hash</strong></p><blockquote><p>内部编码</p></blockquote><p>hash的内部编码有两种：</p><ul><li>ziplist：当field-value元素的个数小于<code>hash-max-ziplist-entries</code>配置（默认512个），且所有值都小于<code>hash-max-ziplist-value</code>配置（默认64字节），redis会使用ziplist作为hash内部实现，ziplist使用更加紧凑的结构实现多个元素的连续存储，更加节省内存</li><li>hashtable：当无法满足ziplist条件时，会使用hashtable作为内部实现，因为元素过多或者值占用空间过大，ziplist的读写效率会下降，而hashtable的读写时间复杂度为O(1)，但是hashtable更占用内存</li></ul><blockquote><p>三种缓存用户信息的方式</p></blockquote><ul><li>原生字符串类型，每个属性一个键（通常不会使用这种方法来存用户的信息）</li></ul><figure><img src="https://gitee.com/eddie-lucas/images/raw/master/img/image-20240501192401592.png" alt="" tabindex="0" loading="lazy"><figcaption></figcaption></figure><p>优点：简单直观，每个属性都支持更新操作</p><p>缺点：一个用户的信息占用了太多键，内存占用量大，且用户信息的内聚性比较差</p><hr><ul><li>序列化字符串类型，将用户信息序列化之后，使用一个键存储</li></ul><p>优点：简化编程，如果合理使用序列化，可以提高内存使用效率</p><p>缺点：序列化和反序列化有一定的开销，每次更新用户信息都需要进行反序列化，再进行序列化</p><hr><ul><li>使用hash</li></ul><p>优点：简单直观，如果合理使用，能够减少内存空间的使用</p><p>缺点：要控制hash再ziplist和hashtable两种内部编码的转换，hashtable会消耗更多内存</p><h2 id="list" tabindex="-1"><a class="header-anchor" href="#list" aria-hidden="true">#</a> list</h2><p>list是<strong>有序</strong>的，list中的<strong>元素是可以重复的</strong>，它是一种比较灵活的数据结构，可以用来充当栈或队列</p><figure><img src="https://gitee.com/eddie-lucas/images/raw/master/img/image-20240501195939145.png" alt="" tabindex="0" loading="lazy"><figcaption></figcaption></figure><blockquote><p>内部编码</p></blockquote><p>list的内部编码有两种：</p><ul><li>ziplist：当field-value元素的个数小于<code>list-max-ziplist-entries</code>配置（默认512个），且所有值都小于<code>list-max-ziplist-value</code>配置（默认64字节），redis会使用ziplist作为hash内部实现，ziplist使用更加紧凑的结构实现多个元素的连续存储，更加节省内存</li><li>linkedlist：当无法满足ziplist条件时，会使用linkedlist作为内部实现</li><li>quicklist：以ziplist为节点的linkedlist，结合了二者的优势</li></ul><blockquote><p>使用场景</p></blockquote><ul><li><p>消息队列</p></li><li><p>结合hash实现文章列表</p></li></ul><p>每个用户有自己的文章列表，现在需要分页展示文章列表，可以使用hash+list，因为list不仅是有序的，而且支持按照索引范围获取元素</p><ol><li>每篇文章使用hash结构进行存储，例如每篇文章有title，timestamp，cotent三个属性</li></ol><figure><img src="https://gitee.com/eddie-lucas/images/raw/master/img/image-20240501201024640.png" alt="" tabindex="0" loading="lazy"><figcaption></figcaption></figure><ol start="2"><li>向用户文章列表添加文章，<code>user:{id}:articles</code>作为key</li></ol><figure><img src="https://gitee.com/eddie-lucas/images/raw/master/img/image-20240501201237023.png" alt="" tabindex="0" loading="lazy"><figcaption></figcaption></figure><p>每个用户有一个自己的文章列表</p><h2 id="set" tabindex="-1"><a class="header-anchor" href="#set" aria-hidden="true">#</a> set</h2><p>set是无序的，元素是不可重复的，可以进行一些集合运算，以及随机获取/取出元素之类的操作</p><blockquote><p>内部编码</p></blockquote><ul><li>intset：整数集合，当集合中的元素都是整数且元素的个数小于<code>set-max-intset-entries</code>配置（默认512个），会采用intset作为内部实现，从而减少内存的使用</li><li>hashtable：当无法满足intset，会采用hashtable作为内部实现</li></ul><blockquote><p>使用场景</p></blockquote><p>共同标签tag，生成随机数抽奖</p><h2 id="zset" tabindex="-1"><a class="header-anchor" href="#zset" aria-hidden="true">#</a> zset</h2><p>zset是有序集合，保留了集合的不能有重复元素的特性，同时是可以排序的，可以为每个元素设置score作为排序依据，<strong>zset中元素是不能重复的，但是score是可以重复的</strong></p><figure><img src="https://gitee.com/eddie-lucas/images/raw/master/img/image-20240501202645097.png" alt="" tabindex="0" loading="lazy"><figcaption></figcaption></figure><blockquote><p>内部编码</p></blockquote><p>zset内部编码有两种实现方式：</p><ul><li>ziplist：当zset元素个数小于<code>zset-max-ziplist-entries</code>配置（默认128个），同时，每个元素的值都小于<code>zset-max-ziplist-value</code>配置（默认64字节）会采用ziplist作为内部实现</li><li>skiplist：当ziplist条件不满足时，以此作为内部实现，因为此时ziplist读写效率下降</li></ul><blockquote><p>使用场景</p></blockquote><figure><img src="https://gitee.com/eddie-lucas/images/raw/master/img/image-20240501203258488.png" alt="" tabindex="0" loading="lazy"><figcaption></figcaption></figure><h2 id="理解内存" tabindex="-1"><a class="header-anchor" href="#理解内存" aria-hidden="true">#</a> 理解内存</h2><p>redis数据存在于内存之中，如何高效的利用redis内存非常重要，想要高效的利用内存，首先需要理解redis内存消耗在哪里，如何管理内存，最后才能考虑如何优化内存</p><h3 id="内存消耗" tabindex="-1"><a class="header-anchor" href="#内存消耗" aria-hidden="true">#</a> 内存消耗</h3><p>理解redis内存，首先要掌握redis内存消耗在哪些方面，有些内存的消耗是必不可少的，而有些内存消耗是可以通过参数调整和合理使用来规避内存浪费，<strong>内存消耗可以分为进程内存消耗和子进程内存消耗</strong></p><blockquote><p>内存使用统计：<code>info memory</code>命令获取内存相关指标</p></blockquote><div class="language-bash line-numbers-mode" data-ext="sh"><pre class="language-bash"><code><span class="token number">127.0</span>.0.1:637<span class="token operator"><span class="token file-descriptor important">9</span>&gt;</span> info memory
<span class="token comment"># Memory# Redis 保存数据申请的内存空间</span>
used_memory:848136
used_memory_human:828.26K
<span class="token comment"># 操作系统分配给 Redis 进程的内存空间</span>
used_memory_rss:2449408
used_memory_rss_human:2.34M
<span class="token comment"># Redis 进程在运行过程中占用的内存峰值</span>
used_memory_peak:910608
used_memory_peak_human:889.27K
<span class="token comment"># 使用内存达到峰值内存的百分比, 即(used_memory/ used_memory_peak) *100%</span>
used_memory_peak_perc:93.14%
<span class="token comment"># Redis为了维护数据集的内部机制所需的内存开销, 包括所有客户端输出缓冲区, 查询缓冲区, AOF重写缓</span>
used_memory_overhead:836278
<span class="token comment"># Redis服务器启动时消耗的内存</span>
used_memory_startup:786488
<span class="token comment"># 数据占用的内存大小, 即used_memory - used_memory_overhead</span>
used_memory_dataset:11858
<span class="token comment"># 数据占用的内存大小的百分比# 100%*(used_memory_dataset/(used_memory-used_memory_startup))</span>
used_memory_dataset_perc:19.24%
<span class="token comment"># 系统内存</span>
total_system_memory:2095968256
total_system_memory_human:1.95G
<span class="token comment"># Lua脚本存储占用的内存used_memory_lua:37888</span>
used_memory_lua_human:37.00K
<span class="token comment"># Redis实例的最大内存配置maxmemory:0</span>
maxmemory_human:0B
<span class="token comment"># 淘汰策略</span>
maxmemory_policy:noeviction
<span class="token comment"># 内存的碎片率, used_memory_rss/used_memory</span>
mem_fragmentation_ratio:2.89
<span class="token comment"># 内存分配器</span>
mem_allocator:jemalloc-4.0.3
<span class="token comment"># 表示没有活动的defrag任务正在运行, 1表示有活动的defrag任务正在运行（defrag:表示内存碎片整理）</span>
active_defrag_running:0
<span class="token comment"># 表示redis执行lazy free操作,在等待被实际回收内容的键个数</span>
lazyfree_pending_objects:0
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><figure><img src="https://gitee.com/eddie-lucas/images/raw/master/img/image-20240501205205533.png" alt="" tabindex="0" loading="lazy"><figcaption></figcaption></figure><p>重点关注指标：</p><ul><li><p><code>used_memory_rss</code>：操作系统显示redis进程占用的物理内存总量</p></li><li><p><code>used_memory</code>：存储所有数据的内存占用量</p></li><li><p><code>mem_fragmentation_ratio</code>：内存碎片率，used_memory_rss/used_memory，<strong>比值在1-1.5比较健康</strong></p><ul><li>mem_fragmentation_tatio&gt;1：说明多出的部分内存并没有用于数据存储，而是被内存碎片消耗，如果两者差别很大，说明碎片化严重</li><li>mem_fragmentation_tatio&lt;1：这种情况一般出现在操作系统吧redis内存swap交换到硬盘导致，由于硬盘读写速度远远慢于内存，redis性能会变得很差</li></ul></li></ul><blockquote><p>进程内存消耗</p></blockquote><p>redis进程内存消耗主要包括：自身内存+对象内存+缓冲内存+内存碎片，redis空进程自身内存消耗非常小，可以忽略不计，主要介绍其他3种内存消耗</p><figure><img src="https://gitee.com/eddie-lucas/images/raw/master/img/image-20240501211657415.png" alt="" tabindex="0" loading="lazy"><figcaption></figcaption></figure><ol><li>对象内存</li></ol><p><strong>对象内存是Redis内存占用最大的一块，存储着用户所有的数据</strong>。Redis所有的数据都采用key-value数据类型，每次创建键值对时，至少创建两个类型对象：key对象和value对象。<strong>对象内存消耗可以简单理解为sizeof（keys）</strong><strong>+sizeof（values）</strong>。键对象都是字符串，在使用Redis时很容易忽略键对内存消耗的影响，应当避免使用过长的键</p><ol start="2"><li>缓冲内存</li></ol><p>缓冲内存主要包括：客户端缓冲区，复制积压缓冲区，AOP缓冲区</p><ol start="3"><li>内存碎片</li></ol><p>redis默认的内存分配器采用jemalloc，可选的分配器还有：glibc，tcmalloc，有了内存分配器可以更好的管理和重复利用内存，内存碎片就是内存分配器给对象分配内存后，空间太小，不能够分配给其他对象存储的内存空间。</p><p>当存储的数据长短差异较大时，以下场景容易出现内存碎片问题：</p><ul><li>频繁做更新操作，例如频繁对已存在的键执行append，setrange等更新操作</li><li>大量过期key删除，key对象过期删除后，释放的空间无法得到充分利用，导致碎片化率上升</li></ul><blockquote><p>出现高内存碎片常见解决方式</p></blockquote><ul><li>数据对齐：在条件允许情况下尽量做数据对齐，比如数据尽量采用数字类型或固定长度字符串，但是这要视具体业务而定，有些场景无法做到</li><li>安全重启：重启节点可以做到内存碎片重新整理，因此可以利用高可用架构，如Sentinel或Cluster，将碎片率过高的主节点转换为从节点，进行安全重启</li></ul><h2 id="哨兵模式" tabindex="-1"><a class="header-anchor" href="#哨兵模式" aria-hidden="true">#</a> 哨兵模式</h2><p>redis主从复制模式存在的问题：</p><ul><li>一旦主节点出现故障，需要手动将从节点设置为主节点，同时需要修改应用端的主节点地址，还需要命令其他从节点去复制新的主节点，整个过程都需要人工干预</li><li>主节点的写能力受到了单机的限制</li><li>主节点的存储能力受到了单机的限制</li></ul><blockquote><p>为了解决上面的问题，哨兵模式诞生了</p></blockquote><p>哨兵模式：当主节点出现故障，redis sentinel能自动完成故障发现和故障转移，并通知应用方，哨兵模式基本结构如下</p><figure><img src="https://gitee.com/eddie-lucas/images/raw/master/img/image-20240503132132594.png" alt="" tabindex="0" loading="lazy"><figcaption></figcaption></figure><p>整个故障转移处理有4个步骤：</p><ol><li>主节点出现故障，两个从节点与主节点失去连接，主从复制失败</li><li>每个sentinel节点定期监控，发现主节点发生了故障</li><li>多个sentinel节点对主节点的故障表示赞同，选举出一个sentinel节点作为领导者负责故障转移</li><li>sentinel领导者节点执行故障转移： <ol><li>将某个从节点设置为主节点</li><li>告诉其他从节点，主节点变更以及为其他从节点配置新的主节点的信息</li><li>通知应用方主节点变更</li><li>等原来的主节点恢复后，将其设置为从节点</li></ol></li></ol><h3 id="redis-sentinel原理" tabindex="-1"><a class="header-anchor" href="#redis-sentinel原理" aria-hidden="true">#</a> redis sentinel原理</h3><p>redis sentinel实现原理主要包含4个方面：</p><ul><li>三个定时任务</li><li>主观下线和客观下线</li><li>sentinel领导者选举</li><li>故障转移</li></ul><blockquote><p>3个定时任务</p></blockquote><ol><li>每隔10s，每个sentinel节点会向主节点和从节点发送info命令获取最新的拓扑结构</li></ol><figure><img src="https://gitee.com/eddie-lucas/images/raw/master/img/image-20240503134639769.png" alt="" tabindex="0" loading="lazy"><figcaption></figcaption></figure><ol start="2"><li>每隔2s，每个sentinel节点会向redis数据节点的<code>_sentinel_:hello</code>频道上<strong>发送该sentinel节点对于主节点的判断以及当前sentinel节点的信息</strong>，同时每个sentinel节点也会订阅该频道，来了解其他sentinel节点以及它们对主节点的判断，该定时任务完成了两个工作： <ol><li>发现新的sentinel节点：通过订阅主节点的<code>_sentinel_:hello</code>了解其他sentinel节点信息，如果是新加入的sentinel节点，将该sentinel节点信息保存起来，并与该sentinel节点创建连接</li><li>sentienl节点之间交换主节点的状态，作为后i面客观下线以及领导者选举的依据</li></ol></li></ol><figure><img src="https://gitee.com/eddie-lucas/images/raw/master/img/image-20240503135330689.png" alt="" tabindex="0" loading="lazy"><figcaption></figcaption></figure><ol start="3"><li>每隔1s，每隔sentinel节点会向主节点、从节点、其余sentinel节点发送一条<code>ping</code>命令做一次心跳检测，来确认这些节点当前是否可达</li></ol><figure><img src="https://gitee.com/eddie-lucas/images/raw/master/img/image-20240503135406070.png" alt="" tabindex="0" loading="lazy"><figcaption></figcaption></figure><blockquote><p>主观下线</p></blockquote><p>当执行第3个定时任务（心跳检测）时，当这些节点超过<code>down-after-milliseconds</code>没有进行回复，sentinel节点就会对该节点做失败判定，这个行为就叫<strong>主观下线</strong>，主观下线是当前sentinel节点的主观判断，是一家之言，存在误判的可能</p><figure><img src="https://gitee.com/eddie-lucas/images/raw/master/img/image-20240503140210859.png" alt="" tabindex="0" loading="lazy"><figcaption></figcaption></figure><blockquote><p>客观下线</p></blockquote><p><strong>当Sentinel主观下线的节点是主节点时</strong>，该Sentinel节点会通过<code>sentinel is-master-down-by-addr</code>命令向其他Sentinel节点询问对主节点的判断，当超过<code>quorum</code>个数（通常过半），Sentinel节点认为主节点确实有问题，这时该Sentinel节点会做出客观下线的决定，这样客观下线的含义是比较明显了，也就是大部分Sentinel节点都对主节点的下线做了同意的判定，那么这个判定就是客观的，<strong>从节点、sentinel节点主观下线后，是没有故障转移操作的</strong></p><figure><img src="https://gitee.com/eddie-lucas/images/raw/master/img/image-20240503141347500.png" alt="" tabindex="0" loading="lazy"><figcaption></figcaption></figure><blockquote><p>领导者sentinel节点选举</p></blockquote><p>故障转移的工作只需要一个sentinel节点来完成就可以了，不需要那么多sentinel节点，所以多个sentinel节点之间会做一个领导者选举工作，选举出的节点进行故障转移工作，redis使用了raft算法实现领导者选举，领导者选举大致思路如下：</p><ol><li>每个在线的sentinel节点都有资格成为领导者，当他确认主节点主观下线后，会向其他sentinel节点发送<code>sentinel is-master-down-by-addr</code>命令，要求其他节点投票自己为领导者</li><li>收到命令的sentinel节点，如果没有投过票，将投票，否则拒绝</li><li>如果该sentinel节点发现自己的票数已经&gt;=max(quorum,num(sentinels)/2+1)，它将称为领导者</li><li>如果此过程没有选举出领导者，将进行下一次选举</li></ol><blockquote><p>故障转移</p></blockquote><ol><li>在从节点列表中选出一个节点作为新的主节点 <ol><li>过滤一些节点，包括主观下线，短线，5s内没回复过sentinel节点的ping命令，与主节点失联超过<code>down-after-milliseconds</code>*10s的节点</li><li>选择从节点优先级最高的，如果不存在，则继续找</li><li>选择复制偏移量最大的从节点（复制最完整的），如果不存在则继续找</li><li>选择runid最小的从节点</li></ol></li></ol><figure><img src="https://gitee.com/eddie-lucas/images/raw/master/img/image-20240503150207673.png" alt="" tabindex="0" loading="lazy"><figcaption></figcaption></figure><ol start="2"><li><p>sentinel领导节点将选出的从节点设置为主节点</p></li><li><p>sentinel领导节点向剩余从节点发送命令，让他们成为新主节点的从节点</p></li><li><p>sentinel节点集合会将原来的主节点更新为从节点，并保持着对其关注，当其恢复后命令它去复制新的主节点</p></li></ol></div><!--[--><!----><!--]--><footer class="page-meta"><div class="meta-item edit-link"><a href="https://github.com/eddie-lucas/edit/main/src/readNote/redis/Redis开发与运维.md" rel="noopener noreferrer" target="_blank" aria-label="编辑此页" class="nav-link label"><!--[--><svg xmlns="http://www.w3.org/2000/svg" class="icon edit-icon" viewBox="0 0 1024 1024" fill="currentColor" aria-label="edit icon"><path d="M430.818 653.65a60.46 60.46 0 0 1-50.96-93.281l71.69-114.012 7.773-10.365L816.038 80.138A60.46 60.46 0 0 1 859.225 62a60.46 60.46 0 0 1 43.186 18.138l43.186 43.186a60.46 60.46 0 0 1 0 86.373L588.879 565.55l-8.637 8.637-117.466 68.234a60.46 60.46 0 0 1-31.958 11.229z"></path><path d="M728.802 962H252.891A190.883 190.883 0 0 1 62.008 771.98V296.934a190.883 190.883 0 0 1 190.883-192.61h267.754a60.46 60.46 0 0 1 0 120.92H252.891a69.962 69.962 0 0 0-69.098 69.099V771.98a69.962 69.962 0 0 0 69.098 69.098h475.911A69.962 69.962 0 0 0 797.9 771.98V503.363a60.46 60.46 0 1 1 120.922 0V771.98A190.883 190.883 0 0 1 728.802 962z"></path></svg><!--]-->编辑此页<span><svg class="external-link-icon" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path><polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg><span class="external-link-icon-sr-only">open in new window</span></span><!----></a></div><div class="meta-item git-info"><div class="update-time"><span class="label">上次编辑于: </span><!----></div><div class="contributors"><span class="label">贡献者: </span><!--[--><!--[--><span class="contributor" title="email: lucasstark@qq.com">Lucas</span><!--]--><!--]--></div></div></footer><nav class="vp-page-nav"><!----><a aria-label="Redis设计与实现" class="vp-link nav-link next nav-link next" href="/readNote/redis/Redis%E8%AE%BE%E8%AE%A1%E4%B8%8E%E5%AE%9E%E7%8E%B0.html"><div class="hint">下一页<span class="arrow end"></span></div><div class="link">Redis设计与实现<!----></div></a></nav><div id="comment" class="giscus-wrapper input-top" style="display:none;"><div class="loading-icon-wrapper" style="display:flex;align-items:center;justify-content:center;height:96px"><svg xmlns="http://www.w3.org/2000/svg" width="48" height="48" preserveAspectRatio="xMidYMid" viewBox="25 25 50 50"><animateTransform attributeName="transform" type="rotate" dur="2s" keyTimes="0;1" repeatCount="indefinite" values="0;360"></animateTransform><circle cx="50" cy="50" r="20" fill="none" stroke="currentColor" stroke-width="4" stroke-linecap="round"><animate attributeName="stroke-dasharray" dur="1.5s" keyTimes="0;0.5;1" repeatCount="indefinite" values="1,200;90,200;1,200"></animate><animate attributeName="stroke-dashoffset" dur="1.5s" keyTimes="0;0.5;1" repeatCount="indefinite" values="0;-35px;-125px"></animate></circle></svg></div></div><!--[--><!----><!--]--><!--]--></main><!--]--><footer class="vp-footer-wrapper"><div class="vp-footer">记录，分享自己的学习过程.....</div><div class="vp-copyright">Copyright © 2025 小刘Learning</div></footer></div><!--]--><!--]--><!----><!----><!--]--></div>
    <script type="module" src="/assets/app-d83e1369.js" defer></script>
  </body>
</html>
